package com.lonwin.mes2u8.domain.u8;

import com.google.common.collect.Lists;
import com.lonwin.mes2u8.common.CommonValue;
import com.lonwin.mes2u8.domain.mes.MesSaleOrderInfo;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author DELL
 * @Date 2023-12-14 11:09
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class U8SaleOrderAddParam {
    // 订单号
    private String code;
    // 日期
    private Date date;
    // 业务类型
    private String businesstype;
    // 销售类型编码
    private String typecode;
    // 销售类型
    private String typename;
    // 单据状态
    private String  state;
    // 客户编码
    private String custcode;
    // 客户名称
    private String cusname;
    // 客户简称
    private String cusabbname;
    // /部门编码
    private String deptcode;
    // 部门名称
    private String deptname;
    // 人员编码
    private String  personcode;
    // 人员
    private String personname;
    // 预完工日期
    private Date dpremodatebt;
    // 预发货日期
    private Date dpredatebt;
    // 发货地址
    private String sendaddress;
    // 联系人
    private String ccusperson;
    // 联系人编码
    private String ccuspersoncode;
    // 收货地址编码
    private String caddcode;
    // 币种
    private String currency;
    // 汇率
    private BigDecimal currencyrate;
    // 税率，默认16
    private BigDecimal taxrate;
    // 单据头自定义项1
    private String define1;
    // 单据头自定义项2
    private String  define2;
    // 单据头自定义项3
    private String define3;
    // 单据头自定义项4
    private Date define4;
    // 单据头自定义项5
    private Integer define5;
    // 单据头自定义项6
    private Date define6;
    // 单据头自定义项7
    private BigDecimal define7;
    // 单据头自定义项8
    private String define8;
    // 单据头自定义项9
    private String define9;
    // 单据头自定义项10
    private String define10;
    // 单据头自定义项11
    private String define11;
    // 单据头自定义项12
    private String define12;
    // 单据头自定义项13
    private String define13;
    // 单据头自定义项14
    private String define14;
    // 单据头自定义项15
    private Integer define15;
    // 单据头自定义项16
    private BigDecimal define16;
    // 备注
    private String memo;
    // 制单人
    private String  maker;
    // 审核人
    private String verifier;
    // 关闭人
    private String closer;
    // 订单entry
    private List<SaleOrderEntry> entry;

    public static U8SaleOrderAddParam buildU8SaleOrderAddParamBySaleOrderInfoList(List<MesSaleOrderInfo> mesSaleOrderInfoList) {
        if (mesSaleOrderInfoList.isEmpty()) {
            return null;
        }

        MesSaleOrderInfo first = mesSaleOrderInfoList.get(0);

        if (Objects.isNull(first.getMoney())) {
            return null;
        }

        List<SaleOrderEntry> entries = Lists.newArrayList();
        BigDecimal totalWeight = BigDecimal.ZERO;

        for (int i = 0; i < mesSaleOrderInfoList.size(); i++) {
            MesSaleOrderInfo info = mesSaleOrderInfoList.get(i);
            if (Objects.isNull(info.getFinishedWeight())) {
                info.setFinishedWeight(BigDecimal.ONE);
            }
            if (StringUtils.isBlank(info.getTasksNum())) {
                info.setTasksNum("1");
            }
            if (Objects.isNull(info.getMoney())) {
                info.setMoney(BigDecimal.ZERO);
            };

            BigDecimal u8Num = info.getSingleWeight().divide(new BigDecimal("1000"), 6, RoundingMode.HALF_UP).multiply(new BigDecimal(info.getTasksNum()));
            totalWeight = totalWeight.add(u8Num);
            SaleOrderEntry orderEntry = SaleOrderEntry.builder()
                    .inventorycode(info.getProductIdentification().replace("-", ""))
                    .inventoryname(info.getFlangeName())
                    .unitcode(CommonValue.U8_INVENTORY_UNIT_CODE_2)
                    .assistantunit(CommonValue.U8_INVENTORY_UNIT_CODE_2)
                    .unitrate(info.getSingleWeight().divide(new BigDecimal("1000"), 6, RoundingMode.HALF_UP))
                    .quantity(u8Num)
                    .num(new BigDecimal(info.getTasksNum()))
                    .invstd(info.getSpec())
                    .rowno(i+1)
                    .build();
            entries.add(orderEntry);
        }

        BigDecimal cost = first.getMoney().divide(totalWeight, 6, RoundingMode.HALF_UP);
        entries.forEach(entry -> entry.setTaxunitprice(cost));

        return U8SaleOrderAddParam.builder()
                .code(first.getBno()).businesstype(CommonValue.U8_SALE_ORDER_BUSINESS_TYPE)
                .typecode("01").custcode(first.getCno()).cusname(first.getCno()).deptcode("03")
                .taxrate(new BigDecimal("13")).currency("人民币").currencyrate(new BigDecimal("1"))
                .define7(first.getMoney())
                .maker(CommonValue.U8_SALE_ORDER_MAKER)
                .entry(entries)
                .build();
    }
}
